// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import ElementUI from 'element-ui'
import 'babel-polyfill'
import 'element-ui/lib/theme-chalk/index.css'
import 'font-awesome/css/font-awesome.min.css'
import './assets/iconfont/iconfont.css'
import './assets/iconfont2/iconfont.css'
import '../static/video/video-js.css'
import 'vue2-animate/dist/vue2-animate.min.css'
import mouseMenu from 'vue-mouse-menu'
Vue.use(mouseMenu)
import store from '../src/store/store'
import router from './router'
import {$url,Axios} from './api'
import func from './assets/js/common'
import VueZtree from "ztree-for-vue";
Vue.use(VueZtree)

import uploader from 'vue-simple-uploader'

Vue.use(uploader);

// connect to ipfs daemon API server
//var ipfs = ipfsClient('localhost', '5001', { protocol: 'http' }) // leaving out the arguments will default to these values

// or connect with multiaddr
//var ipfs = ipfsClient('/ip4/127.0.0.1/tcp/5001')

// or using options
// var ipfs = ipfsClient({ host: '192.168.11.113', port: '5001', protocol: 'http' })

// or specifying a specific API path
// var ipfs = ipfsClient({ host: '192.168.11.113', port: '5001', 'api-path': '/api/v0/' })

// import rightMenu from "rightMenu";
// Vue.use(rightMenu);
Vue.prototype.$BaseUrl = $url;
Vue.prototype.$http = Axios
Vue.prototype.$store = store
Vue.prototype.func = func
Vue.prototype.$getFileSize = function (size) {
  if (!size) {
    return "";
  }else if (size == null) {
    return "暂无数据";
  }else if(size == '-'){
    return "-";
  }
  var num = 1024.0; //byte
  if (size < num) return size + "B";
  if (size < Math.pow(num, 2)) return (size / num).toFixed(2) + "K"; //kb
  if (size < Math.pow(num, 3))
    return (size / Math.pow(num, 2)).toFixed(2) + "M"; //M
  if (size < Math.pow(num, 4))
    return (size / Math.pow(num, 3)).toFixed(2) + "G"; //G
}
Vue.prototype.$abslength=function (str) {
  return str.replace(/[^\x00-\xff]/gi, "**").length;
};
Vue.prototype.$getFontShowLength=function(str, emit){
  let realLength=str.replace(/[^\x00-\xff]/gi, "**").length;
  if (realLength <= emit) {
    return str.length;
  }
  let tmp = 0;
  let pattern = new RegExp("[\u4E00-\u9FA5]+");
  for (let i = 0; i < str.length; i++) {
    let a;
    if (pattern.test(str[i]) == true) {
      tmp += 2;
      a = -1;
    } else {
      tmp++;
      a = 2;
    }
    if (tmp >emit) {
      console.log(i);
      return i;
    }
  }
};
Vue.use(ElementUI)
Vue.config.productionTip = false

router.beforeEach((to, from, next) => {
  let {path} = to
  console.log(path);
  if (path === '/login') {
    var storage = window.localStorage;
    storage.clear()
  }
  // console.log(path)
  let user = localStorage.getItem('authToken')
  let isAdmin = localStorage.getItem("isAdministrators")

  if (to.matched.some(record => record.meta.requireAuth)){ // 判断该路由是否需要登录权限
    if (user) { // 判断当前的user_id是否存在 ； 登录存入的user_id
      if(to.matched.some(record => record.meta.isAdmin)){
        if(isAdmin == 1){
          next();
        }else{
          next({
            path: '/FolderPage/FileDetails',
            query: {redirect: to.fullPath} // 将要跳转路由的path作为参数，传递到登录页面
          })
        }
      }else{
        next();
      }
    } else {
      next({
        path: '/login',
        query: {redirect: to.fullPath} // 将要跳转路由的path作为参数，传递到登录页面
      })
    }
  }
  else {
    next();
  }
})

Vue.prototype.$getApi = function (params) {
  return new Promise((resolve, reject) => {
    Axios.get('/getApi/id', params).then(
      res => {
        resolve(res);
      },
      err => {
        alert('网络错误，该条错误来自于接口检测')
        reject(err);
      }
    )
  })
}

Vue.filter('formatSize', function (size) {
  if (size > 1024 * 1024 * 1024 * 1024) {
    return (size / 1024 / 1024 / 1024 / 1024).toFixed(2) + ' TB'
  } else if (size > 1024 * 1024 * 1024) {
    return (size / 1024 / 1024 / 1024).toFixed(2) + ' GB'
  } else if (size > 1024 * 1024) {
    return (size / 1024 / 1024).toFixed(2) + ' MB'
  } else if (size > 1024) {
    return (size / 1024).toFixed(2) + ' KB'
  }
  return size.toString() + ' B'
});
/* eslint-disable no-new */
new Vue({
  el: '#app',
  store,
  router,
  components: { App },
  template: '<App/>'
})
